//
//  CWQButtonWithBadge.m
//  MobileClinicalOnline
//
//  Created by 陈武琦 on 16/8/8.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "CWQButtonWithBadge.h"

@implementation CWQButtonWithBadge

- (instancetype)initWithFrame:(CGRect)frame image:(NSString *)image
{
    self = [super initWithFrame:frame];
    if (self) {
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [_btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [_btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        [self addSubview:_btn];
        
        _badge = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width -15, 0, 15, 15)];
        _badge.textAlignment = NSTextAlignmentCenter;
        _badge.backgroundColor = [UIColor redColor];
        _badge.textColor = [UIColor whiteColor];
        _badge.adjustsFontSizeToFitWidth = YES;
        _badge.text = @"12";
        _badge.layer.borderColor = [UIColor whiteColor].CGColor;
        _badge.layer.cornerRadius = _badge.frame.size.width/2.0;
        _badge.layer.masksToBounds = YES;
        _badge.hidden = YES;
        [self addSubview:_badge];
    }
    return self;
}

- (void)click:(UIButton *)sender {
    if ([_delegate respondsToSelector:@selector(cwq_clickButton:)]) {
        [_delegate cwq_clickButton:sender];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
